package com.nccsjz.back.customer.web;

import java.sql.SQLException;

import com.nccsjz.back.customer.dao.CustomerDAO;
import com.nccsjz.back.customer.service.CustomerService;
import com.nccsjz.base.BaseAction;
import com.nccsjz.pojo.Customer;
import com.nccsjz.utils.Pager;

@SuppressWarnings("serial")
public class CusAction extends BaseAction {
	// 卡户
	private Customer merch;

	private CustomerService service;
	private long mstat;
	private String mcode;

	public long getMstat() {
		return mstat;
	}

	public void setMstat(long mstat) {
		this.mstat = mstat;
	}

	public String getMcode() {
		return mcode;
	}

	public void setMcode(String mcode) {
		this.mcode = mcode;
	}

	// 存放分页信息
	private Pager pager;
	// 页码
	private int pageNo = 1;
	// 每页多少行
	private int pageSize = 5;
	// 判断标识
	private String flag;
	// 商户ID
	private long id;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Customer getMerch() {
		return merch;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setMerch(Customer merch) {
		this.merch = merch;
	}

	/**
	 * 加卡户
	 * 
	 * @return
	 */
	public String add_Cus() {
		service = new CustomerService();
		try {
			if(service.isExists(merch.getMname())){
				this.addActionError("卡户已经存在!");
				return "register";
			}
			if (service.addCus(merch)) {
				this.addActionMessage("添加成功！");
				return "success";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ERROR;
	}

	/**
	 * 
	 * 修改卡户信息
	 * 
	 * @return
	 */
	public String update_Cus() {
		service = new CustomerService();
		try {
			if (service.updateCus(merch)) {
				this.addActionMessage("修改成功！");
			}
		} catch (Exception e) {
			this.addActionMessage("修改失败！");
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 返回卡户列表
	 * 
	 * @return
	 */
	public String list_Cus() {
		service = new CustomerService();
		try {
			pager = service.getCusList(pageSize, pageNo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 查看商户信息具体内容。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String viewCon_Cus() throws Exception {
		service = new CustomerService();
		 this.merch = service.getCus(id);
		return "view";
	}



	/**
	 * 注销商户
	 * 
	 * @throws SQLException
	 */
	public String del_Cus() throws SQLException {

		service = new CustomerService();

		if (service.deleteCus(id)) {
			return "listf";
		} else {
			return ERROR;
		}

	}
	/**
	 * 根据卡号查询卡户详细信息
	 * @return
	 */
	public String search_Cus(){
		service = new CustomerService();
		try {
			pager = service.getCusList(pageSize, pageNo,merch);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}
	
	/**
	 * initPass_Cus方法用于将卡户密码初始化为6个1
	 * 
	 * @return
	 * @throws SQLException 
	 */
	public String initPass_Cus() throws SQLException{
		service = new CustomerService();
		service.initCustPwd(id);
		this.addActionMessage("卡户的密码已经被初始化为6个1!");
		pager = service.getCusList(pageSize, pageNo);
		return "list";
	}

}
